package net.crazyadam.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.chance.platform.common.tablestruct.SoundLocMode;
import com.chance.platform.common.tools.Constants;

import java.util.List;
import java.util.logging.Handler;

import static com.chance.platform.common.tools.ProtocolConstant.*;

public class DopplerMsgReceiver extends BroadcastReceiver {
    private DopplerActivity doppler;
    private Handler handler;

    public DopplerMsgReceiver() {
        super();
    }

    public DopplerMsgReceiver(DopplerActivity doppler) {
        this.doppler = doppler;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        short type = bundle.getShort(Constants.MSG_CONTENT_KEY); //消息类型
        int fromUser = bundle.getInt(Constants.MSG_UID_KEY);//源 用户id
        if (0 != fromUser)
            try {
                switch (type) {
                    case INTER_WITH_SERV_SOUNDLOC_START_READY_RSP://请求其他邻居打开服务
                        //測定现在频率
                        //开始接收
                        doppler.toBeReady(fromUser);
                        break;
                    case INTER_WITH_SERV_SOUNDLOC_START_RSP: //其他邻居是否均已准备好。
                        int ready = bundle.getInt(Constants.MSG_SUC_KEY);
                        if (1 == ready) {//准备好后，提示可以甩动
                            doppler.readySet(fromUser);
                        }
                        break;
                    case INTER_WITH_SERV_SOUNDLOC_STOP_RESULT_RSP ://通知邻居关闭服务
                        //关闭服务
                        doppler.stopAndSendResult(fromUser);
                        break;
                    case INTER_WITH_SERV_SOUNDLOC_STOP_RSP: //服务期返回排序结果获取
                        //获得结果
                        List<SoundLocMode> list = bundle.getParcelableArrayList(Constants.TABLE_SERVER_SOUNDLOC);
                        //显示返回list
                        if (1 == list.size()) {
                            doppler.resultSet(list.get(0));
                        }
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

    }
}
